package com.uam.core.lk.utils.gps;


/**
 * WGS-84：是国际标准，GPS坐标（Google Earth使用、或者GPS模块）
 * GCJ-02：中国坐标偏移标准，Google Map、高德、腾讯使用
 * BD-09：百度坐标偏移标准，Baidu Map使用
 * Mercator：墨卡托投影
 *
 * @author: heguanglong
 * @date: 2020-12-29
 */
public class GPSFormat {

    public static final double PI = 3.14159265358979324D;
    public static final double X_PI = 3.14159265358979324D * 3000.0 / 180.0;


    /**
     * BD09坐标转换GCJ02坐标
     *
     * @param bdLng
     * @param bdLat
     * @return
     */
    public static GpsPoint bd09ToGcj02(double bdLng, double bdLat) {
        double x = bdLng - 0.0065d, y = bdLat - 0.006d;
        double z = Math.sqrt(x * x + y * y) - 0.00002d * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) - 0.000003d * Math.cos(x * X_PI);
        double gcjLon = z * Math.cos(theta);
        double gcjLat = z * Math.sin(theta);
        return new GpsPoint(gcjLon, gcjLat);
    }


    /**
     * WGS84坐标系：即地球坐标系，国际上通用的坐标系。
     * 设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
     */
    public static GpsPoint wgs84ToGcj02(double wgsLng, double wgsLat) {
        if (outOfChina(wgsLng, wgsLat)) {
            return new GpsPoint(wgsLng, wgsLat);
        }
        GpsPoint d = delta(wgsLng, wgsLat);
        return new GpsPoint(d.getLng() + wgsLng, d.getLat() + wgsLat);
    }


    private static boolean outOfChina(double lng, double lat) {
        if (lng < 72.004 || lng > 137.8347) {
            return true;
        }
        if (lat < 0.8293 || lat > 55.8271) {
            return true;
        }
        return false;
    }

    private static GpsPoint delta(double lng, double lat) {
        //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
        double a = 6378245.0;
        //  ee: 椭球的偏心率。
        double ee = 0.00669342162296594323;
        double dLat = transformLat(lng - 105.0, lat - 35.0);
        double dLon = transformLon(lng - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * PI);
        return new GpsPoint(dLon, dLat);
    }

    private static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }
}